/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lbim.server.main;

import java.sql.SQLException;
import java.util.HashMap;
import lbim.server.data.AccountManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;


/**
 *
 * @author Jestan
 */
public class LocateIMServer {

    private final static Logger logger = LoggerFactory.getLogger(LocateIMServer.class);

    public final static String VERSION = "0.2";

    //user, sid
    public static HashMap<String,String> currentUsers;

    public static HashMap<String,String> currentSessions;

    public static AccountManager accountManager;

    static {
        try {
            accountManager = new AccountManager();
            currentUsers = new HashMap<String, String>();
            currentSessions = new HashMap<String, String>();
        } catch (SQLException ex) {
            logger.error("Failed to setup the db",ex);
        } catch (ClassNotFoundException ex) {
            logger.error("Failed to load the emmbeded derby driver", ex);
        }
    }

    public static boolean authenticate(String userName, String password) {
        try {
            return accountManager.isValidAccount(userName, password);
        } catch (SQLException ex) {
            logger.error("Failed to authenticate user:" + userName, ex);
            return false;
        }
    }

    public static void main(String[] args) {
        getApplicationContext();
        logger.info("LocateIm Server has started ...");
    }

    public static ConfigurableApplicationContext getApplicationContext() {
         return new ClassPathXmlApplicationContext("lbim/server/main/serverContext.xml");
    }

}
